import os
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class MainWindow(QWidget):

    def __init__(self):
        super(MainWindow, self).__init__()

        self.main_layout=QVBoxLayout()
        self.show_label=QLabel('任务评价')
        self.btn_layout=QHBoxLayout()
        self.test_btn=QPushButton('Test')
        self.test_btn.clicked.connect(self._test)
        self.change_btn=QPushButton('切换')
        self.change_btn.clicked.connect(self._change)
        self.btn_layout.addWidget(self.test_btn)
        self.btn_layout.addWidget(self.change_btn)
        self.assigned_layout=QHBoxLayout()
        self.assigned_label=QLabel('安排合理度:')
        self.assigned_layout.addWidget(self.assigned_label)
        self.assigned_btns=self.init_toolbtn(self.assigned_layout,self._assinged_click)
        self.workhour_layout=QHBoxLayout()
        self.workhour_label=QLabel('工时合理度:')
        self.workhour_layout.addWidget(self.workhour_label)
        self.workhour_btns=self.init_toolbtn(self.workhour_layout,self._workhour_click)


        self.main_layout.addWidget(self.show_label)
        self.main_layout.addLayout(self.btn_layout)
        self.main_layout.addLayout(self.assigned_layout)
        self.main_layout.addLayout(self.workhour_layout)
        self.setLayout(self.main_layout)

        self.assigned_score='5'
        self.workhour_score='5'

    def _assinged_click(self):
        nicon=QIcon('n.png')
        yicon = QIcon('y.png')
        sender=self.sender().text()
        # print(sender)
        index=int(sender)
        # print(index)
        for btn in self.assigned_btns:
            btn.setIcon(QIcon(nicon))
        for i in range(index):
            self.assigned_btns[i].setIcon(QIcon(yicon))
        self.assigned_score=sender

    def _workhour_click(self):
        nicon = QIcon('n.png')
        yicon = QIcon('y.png')
        sender=self.sender().text()
        index=int(sender)
        for btn in self.workhour_btns:
            btn.setIcon(QIcon(nicon))
        for i in range(index):
            self.workhour_btns[i].setIcon(QIcon(yicon))
        self.workhour_score=sender

    def init_toolbtn(self,layout,func):
        btn_list=[]
        nicon = QIcon('n.png')
        yicon = QIcon('y.png')
        for i in range(5):
            tbtn=QToolButton(self)
            tbtn.clicked.connect(func)
            tbtn.setText(str(i+1))
            # if i==0:
            #     tbtn.setIcon(yicon)
            # else:
            #     tbtn.setIcon(nicon)
            tbtn.setIcon(yicon)
            btn_list.append(tbtn)
            layout.addWidget(tbtn)
        return btn_list

    def _test(self):
        print('安排合理度:',self.assigned_score)
        print('工时合理度:',self.workhour_score)

    def _set_score(self,btn_list,score):
        nicon = QIcon('n.png')
        yicon = QIcon('y.png')
        for btn in btn_list:
            btn.setIcon(QIcon(nicon))
        for i in range(score):
            btn_list[i].setIcon(QIcon(yicon))


    def _change(self):
        data={'shot_name':'AA','workhour':'12','assigned_score':'3','workhour_score':'4'}
        assigned_score=data['assigned_score']
        workhour_score=data['workhour_score']
        self._set_score(self.assigned_btns,int(assigned_score))
        self._set_score(self.workhour_btns,int(workhour_score))


if __name__ == '__main__':
    app=QApplication(sys.argv)
    m=MainWindow()
    m.show()
    sys.exit(app.exec_())